﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net.MVC;
using Buffalo.Tourism.DAL.Models;
using Buffalo.Tourism.DAL;
using Buffalo.Lib.Common;
using System.Xml.Xsl;
using System.Xml;
using System.IO;
using Buffalo.Tourism.Enum;
using log4net;
using Buffalo.Administration.DAL;
using Buffalo.Administration.Enum;

namespace Buffalo.Web.Areas.Tourism.Controllers
{
    public class TourProgramController : BaseController
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public ActionResult TourList()
        {
            return CheckPermission( "TourListView");
        }
        public ActionResult ImportExcelView()
        {
            return this.View();
        }
        public ActionResult ExportExcelView()
        {
            ViewData["IDTour"] = HttpContext.Request["IDTour"] ?? string.Empty;
            return this.View();
        }
        public ActionResult TourEditor()
        {
            ViewData["IDTour"] = HttpContext.Request["IDTour"] ?? string.Empty;
            return CheckPermission("TourListView");
        }
        public ActionResult TourDetailEditor()
        {
            ViewData["IDTourDetail"] = HttpContext.Request["IDTourDetail"] ?? string.Empty;
            return this.View();
        }
        public AjaxStoreResult GetTour(string IDTour)
        {
            if (!string.IsNullOrEmpty(IDTour))
            {
                if (IDTour.Equals("0"))
                {
                    var query = new Tour();
                    query.ID = "0";
                    return new AjaxStoreResult(query, 1);
                }
                else
                {
                    var query = new TourProgramDAL().GetTour(IDTour);
                    int total = query != null ? 1 : 0;
                    return new AjaxStoreResult(query, total);
                }
            }

            return null;
        }

        public AjaxStoreResult GetTourDetail(string IDTourDetail)
        {
            if (!string.IsNullOrEmpty(IDTourDetail))
            {
                var query = new TourProgramDAL().GetTourDetail(IDTourDetail);
                int total = query != null ? 1 : 0;
                return new AjaxStoreResult(query, total);
            }

            return null;
        }
        public AjaxResult DeleteTour(string IDTour)
        {
            AjaxResult response = new AjaxResult();
            try
            {
                new TourProgramDAL().DeleteTour(IDTour);
            }
            catch (Exception e)
            {
                response.ErrorMessage = e.ToString();
            }
            return response;
        }

        public AjaxFormResult SaveTourInfo(FormCollection values)
        {
            AjaxFormResult response = new AjaxFormResult();

            try
            {
                string IDTour = values["id"];
                if (string.IsNullOrEmpty(values["TourCode"]))
                {
                    response.Success = false;
                    response.Errors.Add(new FieldError("TourCode", "TourCode is required"));
                    return response;
                }

                Tour tour;
                TourProgramDAL tourProgramDAL = new TourProgramDAL();
                bool isNew = false;
                if (string.IsNullOrEmpty(IDTour) || IDTour.Equals("0"))
                {
                    tour = new Tour();
                    tour.ID = IDTour;
                    tour.LocationRoleName = GeneralVar.gLoggedRoleName;
                    isNew = true;
                }
                else
                {
                    tour = tourProgramDAL.GetTour(IDTour);
                }
                tour.TourCode = values["TourCode"];
                tour.GuestNum = Util.ToSafeInt(values["GuestNum"]);

                //tour.StartDate = Util.ToSafeDateTime(values["StartDate"]);
                tour.StartDate = Util.GetDateFromDMY(values["StartDate"]);
                tour.EndDate = Util.GetDateFromDMY(values["EndDate"]);

                log.Debug(string.Format("values[StartDate] :  {0}, tour.StartDate : {1}, tour.EndDate : {2} ", values["StartDate"], tour.StartDate, tour.EndDate ));

                if(tour.StartDate  == Util.NullDateTime || tour.EndDate == Util.NullDateTime)
                {
                    log.Error(string.Format("ttt tour.StartDate, tour.EndDate :  {0} {1}", tour.StartDate, tour.EndDate));
                    return response;
                }
                tour.NationalityName = values["NationalityName"];
                tour.LanguageName = values["LanguageName"];
                tour.BrokerName = values["BrokerName"];
                tour.OwnCompanyName = values["OwnCompanyName"];

                tour.RoomNum = Util.ToSafeInt( values["RoomNum"]);
                tour.CreatedDate = Util.ToSafeDateTime(values["CreatedDate"]);

                tourProgramDAL.SaveTour(tour);

                if(isNew)
                    response.ExtraParams["newID"] = tour.ID;
                else
                    response.ExtraParams["newID"] = string.Empty;

            }
            catch (Exception e)
            {
                response.Success = false;
                response.ExtraParams["msg"] = e.Message;
            }

            return response;
        }
        public AjaxFormResult SaveTourFeedback(FormCollection values)
        {
            AjaxFormResult response = new AjaxFormResult();

            try
            {
                string IDTour = values["id"];
                
                TourProgramDAL tourProgramDAL = new TourProgramDAL();
                Tour tour = tourProgramDAL.GetTour(IDTour);
                if (tour == null)
                    return response;

                tour.FeedbackGuestName = values["FeedbackGuestName"];
                tour.FeedbackGuestFrom = values["FeedbackGuestFrom"];
                tour.FeedbackGuestEmail = values["FeedbackGuestEmail"];
                tour.FeedbackGuestJob = values["FeedbackGuestJob"];
                tour.FeedbackNote = values["FeedbackNote"];
                tour.FeedbackOther = values["FeedbackOther"];
                tour.FeedbackTourProgress = values["FeedbackTourProgress"];

                tourProgramDAL.SaveTour(tour);

                response.ExtraParams["newID"] = tour.ID;
            }
            catch (Exception e)
            {
                response.Success = false;
                response.ExtraParams["msg"] = e.Message;
            }

            return response;
        }
        public AjaxStoreResult GetListTourDetail(int limit, int start, string IDTour)
        {
            try
            {
                int total = 0;
                var query = new TourProgramDAL().GetListTourDetail(limit, start, IDTour, out total);
                return new AjaxStoreResult(query, total);
            }
            catch (Exception)
            {
                return new AjaxStoreResult();
            }
        }
        public AjaxStoreResult GetListGuest(int limit, int start, string dir, string sort, string IDTour)
        {
            int total = 0;
            try
            {
                var query = new TourProgramDAL().GetListGuest(limit, start, dir, sort, IDTour, out total);
                return new AjaxStoreResult(query, total);
            }
            catch (Exception)
            {
                return new AjaxStoreResult();
            }
        }
        public AjaxStoreResult GetListFeedback(string dir, string sort, string IDTour)
        {
            int total = 0;
            try
            {
                var query = new TourProgramDAL().GetListFeedback(dir, sort, IDTour, out total);
                return new AjaxStoreResult(query, total);
            }
            catch (Exception)
            {
                return new AjaxStoreResult();
            }
        }

        public AjaxStoreResult GetListFeedbackDetail(string IDFeedback)
        {
            int total = 0;
            try
            {
                var query = new TourProgramDAL().GetListFeedbackDetail(IDFeedback, out total);
                return new AjaxStoreResult(query, total);
            }
            catch (Exception)
            {
                return new AjaxStoreResult();
            }
        }


        public AjaxStoreResult GetShortListTour(int limit, string fromDate, string toDate)
        {
            int total = 0;
            var query = new TourProgramDAL().GetShortListTour(limit, Util.GetDateFromDMY(fromDate), Util.GetDateFromDMY(toDate));
            Tour blank = EntityHelper.CreateBlankEntity<Tour>();
            blank.TourCode = GeneralConstant.ALL_ITEMS;
            query.Insert(0, blank); 
            
            try
            {
                return new AjaxStoreResult(query, total);
            }
            catch (Exception)
            {
                return new AjaxStoreResult();
            }
        }
        public AjaxStoreResult GetListArea(string fromDate, string toDate)
        {
            try
            {
                List<TourDetail> listTourDetail = new TourProgramDAL().GetListTourDetail(string.Empty, Util.GetDateFromDMY(fromDate), Util.GetDateFromDMY(toDate), string.Empty);
                var query = new TourProgramDAL().GetListAreaListFromTourDetail(listTourDetail);
                if (query != null)
                {
                    query.Insert(0, GeneralConstant.ALL_ITEMS);

                    List<SelectListItem> listSelectListItem = new List<SelectListItem>();
                    foreach (string area in query)
                    {
                        listSelectListItem.Add(new SelectListItem { Text = area, Value = area });
                    }
                    //return new AjaxStoreResult(query, total);
                    return new AjaxStoreResult(listSelectListItem, listSelectListItem.Count);
                }
            }
            catch (Exception)
            {
                return new AjaxStoreResult();
            }
            return new AjaxStoreResult(new List<SelectListItem>(), 0);
        }
        public AjaxStoreResult GetListVehicle(string fromDate, string toDate)
        {
            try
            {
                int total = 0;
                List<TourDetail> listTourDetail = new TourProgramDAL().GetListTourDetail(string.Empty, Util.GetDateFromDMY(fromDate), Util.GetDateFromDMY(toDate), string.Empty);
                var query = new TourProgramDAL().GetListVehicleListFromTourDetail (listTourDetail );
                if (query != null)
                {
                    Vehicle blank = EntityHelper.CreateBlankEntity<Vehicle>();
                    blank.Name = GeneralConstant.ALL_ITEMS;
                    query.Insert(0, blank);
                    return new AjaxStoreResult(query, total);
                }
            }
            catch (Exception)
            {
                return new AjaxStoreResult();
            }
            return new AjaxStoreResult(new List<Vehicle>(), 0);
        }

        public AjaxStoreResult GetListTour(int limit, int start, string dir, string sort, string cboCriteria, string txtSearchText)
        {
            int total = 0;
            var query = new TourProgramDAL().GetListTour(limit, start, dir, sort, cboCriteria, txtSearchText, out total);
            try
            {
                return new AjaxStoreResult(query, total);
            }
            catch (Exception)
            {
                return new AjaxStoreResult();
            }
        }
        public AjaxStoreResult SaveTourDetails()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new TourProgramDAL().SaveTourDetails(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }

        public AjaxFormResult SaveTourDetail(string IDTourDetail, FormCollection values)
        {
            AjaxFormResult response = new AjaxFormResult();

            try
            {
                TourDetail tourDetail;
                TourProgramDAL tourProgramDAL = new TourProgramDAL();

                if (string.IsNullOrEmpty(IDTourDetail) || IDTourDetail.Equals("0"))
                {
                    tourDetail = EntityHelper.CreateBlankEntity<TourDetail>();
                }
                else
                {
                    tourDetail = tourProgramDAL.GetTourDetail(IDTourDetail);
                }

                tourDetail.TourDate = Util.ToSafeDateTime(values["TourDate"]);
                tourDetail.DayTime = values["DayTime"];
                tourDetail.Program = values["Program"];

                tourDetail.IDTourGuide = values["IDTourGuide"];
                tourDetail.TourGuideNote = values["TourGuideNote"];

                tourDetail.IDHotel = values["IDHotel"];
                tourDetail.HotelNote = values["HotelNote"];

                tourDetail.IDPartner = values["IDPartner"];
                tourDetail.PartnerNote = values["PartnerNote"];

                tourDetail.IDRestaurant = values["IDRestaurant"];
                tourDetail.RestaurantNote = values["RestaurantNote"];

                tourDetail.IDVehicle = values["IDVehicle"];
                tourDetail.VehicleNote = values["VehicleNote"];

                //tourDetail.IDOrgUnit = values["IDOrgUnit"];

                tourDetail.RoomNum = Util.ToSafeInt( values["RoomNum"]);
                tourDetail.GuestNum = Util.ToSafeInt(values["GuestNum"]);

                tourDetail.Gift1 = Util.ToSafeString(values["Gift1"]);

                tourDetail.LocationRoleName = values["LocationRoleName"];

                tourProgramDAL.SaveTourDetail(tourDetail);

                response.ExtraParams["newID"] = tourDetail.ID.ToString();
            }
            catch (Exception e)
            {
                response.Success = false;
                response.ExtraParams["msg"] = e.Message;
            }

            return response;
        }

        public AjaxStoreResult SaveTours()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new TourProgramDAL().SaveTours(HttpContext.Request["data"], GeneralVar.gLoggedRoleName );
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }

        public AjaxStoreResult SaveGuests()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new TourProgramDAL().SaveGuests(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }

        public AjaxStoreResult SaveFeedbacks()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new TourProgramDAL().SaveFeedbacks(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }

        public AjaxStoreResult SaveFeedbackDetail()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new TourProgramDAL().SaveFeedbackDetails(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
    }
}
